//
//  ZWMainTabBar.swift
//  张伟的微博
//
//  Created by 伟 on 16/2/27.
//  Copyright © 2016年 伟. All rights reserved.
//

import UIKit
//协议有optional 需要添加@objc
@objc protocol ZWMainTabBarDelegate :NSObjectProtocol {
    //按钮被点击的调用方法
    
    optional
    func mainTabBarComposeClick()

}

class ZWMainTabBar: UITabBar {

    //2定义代理属性
    weak var composeDelegate: ZWMainTabBarDelegate?
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置button的frame
        
        let buttonW = frame.width / 5.0
        let buttonH = frame.height + 5
        composeButton.frame = CGRect(x: 0, y: 0, width: buttonW+5, height:buttonH)
        composeButton.center = CGPoint(x: frame.width / 2, y: frame.height/2)
        
    }

    
    
    
    //懒加载button
    lazy var composeButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //为按钮添加点击事件
        button.addTarget(self, action:"buttonClick" , forControlEvents: UIControlEvents.TouchUpInside)
        
       self.addSubview(button)
        
        return button
    }()
    func buttonClick() {
        //composeDelegate?表示属性有值才执行后面的方法
        //mainTabBarComposeClick?() 表示代理要实现这个方法就用代理方法，不实现就不调用
        
        self.composeDelegate?.mainTabBarComposeClick?()
        
    }
}
